/********************************************************************************/
/* 	player.d																	*/
/*------------------------------------------------------------------------------*/
/*	製作		（　゜ワ゜）ノ　／　松久貫太									*/
/*	製作開始　	2008/06/29														*/
/*	MAIL		omega@personal.email.ne.jp										*/
/*	URL		http://nagoya.cool.ne.jp/o_mega										*/
/*																				*/
/*	このソースは「やわらかライセンス」の元で配布されています。					*/
/*-更新履歴---------------------------------------------------------------------*/
/*	2007/--/--																	*/
/*-その他-----------------------------------------------------------------------*/
/*	なし																		*/
/********************************************************************************/

/* ライブラリのインポート *******************************************************/
import keypad;
import hell2;
import actor;
import state;
import vector;
import defines;
import std.math;

import shape;

Player player = null;

class Player : Actor{
	int timer;
	Keypad keypad;
	Vec3 rotate;
	Vec3 rotsp;
	PlayerShotPool shotpool;
	
	enum DIRC{
		LEFT  = -1,
		RIGHT =  1,
	}
	
	DIRC dirc;
	double visual_dirc;
	
	static double PLAYER_SIZE = 15;
	static double PLAYER_SPEED = 3;
	static double PLAYER_ROT_SPEED = 0.3;
	
	static int SHOT_MAX = 256;
	
	this(){
		shotpool = new PlayerShotPool( SHOT_MAX );
		
		position = new Vec3();
		position.z = 0;//CHAR_Z_SURFACE;
		rotate = new Vec3();
		rotsp = new Vec3();
		
		dirc = DIRC.RIGHT;
		visual_dirc = 0;
		timer = 0;
		keypad = new Keypad();
	}
	
	void move(){
		timer++;
		
		// 移動関係
		if( keypad.isPressLeft() ){
			position.x -= PLAYER_SPEED;
			rotsp.x -= PLAYER_ROT_SPEED;
			dirc = DIRC.LEFT;
		}
		if( keypad.isPressRight() ){
			position.x += PLAYER_SPEED;
			rotsp.x += PLAYER_ROT_SPEED;
			dirc = DIRC.RIGHT;
		}
		if( keypad.isPressDown() ){
			position.y += PLAYER_SPEED;
			rotsp.y += PLAYER_ROT_SPEED;
		}
		if( keypad.isPressUp() ){
			position.y -= PLAYER_SPEED;
			rotsp.y -= PLAYER_ROT_SPEED;
		}
		
		rotate += rotsp;
		rotsp *= 0.99;
		visual_dirc = visual_dirc * 0.85 + (cast(double)dirc * 90 + 90) * 0.15;
		
		// 攻撃
		if( keypad.isPressEnter() ){
			PlayerShot s = shotpool.getInstance();
			if( s ){
				s.set( position , new Vec3( cast(int)dirc * 20 ,0,0 ) );
			}
		}
		
		// システム
		shotpool.move();
	}
	
	void draw(DRAW_PRIORITY dp = DRAW_PRIORITY.NORMAL){
		glPushMatrix();
		{
			glTranslatef( position.x , position.y , position.z );
			glRotatef( 0 , visual_dirc , 0 );
			
			// 本体
			glScalef( PLAYER_SIZE , PLAYER_SIZE , PLAYER_SIZE );
			glColor4f( 1 , 1 , 1 , 1 );
			resetCullFace();
			drawBillBoard( "player" , 0 , 0 , 64 , 64 );
			setCullFace();
		}
		glPopMatrix();
		
		// マーカー
		glPushMatrix();
		{
			glTranslatef( position.x , position.y , position.z );
			glRotatef( 0 , visual_dirc , 0 );
			glTranslatef( -40.0 , 0 , 0 );
			glRotatef( 0 , 0 , 180 );
			glRotatef( timer * 2.0 , 0 , 0 );
			glScalef( 10 , 10 , 10 );
			glColor4f(0.5 , 1.0 , 0.75 , 0.5 );
			NeedleShape.drawWire();
		}
		glPopMatrix();
		
		shotpool.draw();
	}
}

/*********************************************************************************/

class PlayerShot : Actor{
	int timer;
	Vec3 speed;
	
	static int ALIVE_INTERVAL = 120;
	
	this(){
		position = new Vec3();
		position.z = 0;//CHAR_Z_SURFACE;
		speed = new Vec3();
		timer = 0;
	}
	
	void set( Vec3 pos , Vec3 sp ){
		timer = 0;
		position.set( pos );
		speed.set( sp );
		exist = true;
	}
	
	void move(){
		timer++;
		if( timer > ALIVE_INTERVAL ){
			// 時間死
			exist = false;
		}
		
		position += speed;
	}
	
	void draw(DRAW_PRIORITY dp = DRAW_PRIORITY.NORMAL){
		glPushMatrix();
		{
			glTranslatef( position.x , position.y , position.z );
			
			double angle = atan2( speed.y , speed.x ) / std.math.PI * 180;
			
			glColor4f( 1.0 , 1.0 , 1.0 , 0.5);
			glRotatef( 0 , 0 , angle );
			glScalef( 20 , 10 , 10 );
			NeedleShape.drawWire();
		}
		glPopMatrix();
	}
}

class PlayerShotPool : ActorPool !(PlayerShot){
	this(int n){
		super(n);
	}
}